import { resolve } from 'path';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from 'nestjs-config';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from "./interceptor/logging.interceptor";
import redis from './config/redis';
import { LoginMiddleware } from './middleware/login.middleware';
import { RedisUtils } from './utils/redis.utils';
import { RedisModule } from 'nestjs-redis'

// import location
import { PayWaterModule } from './modules/payWater/pay-water.module';

import { AddressModule } from './modules/address/address.module';

import { ClassificationModule } from "./modules/classification/classification.module";
import { CartModule } from "./modules/cart/cart.module";
import { OrderModule } from "./modules/order/order.module";
import { ProductModule } from './modules/product/product.module'
import { FrontRequestLogModule } from "./modules/frontRequestLog/front-request-log.module";
import { NavConfigModule } from './modules/navConfig/nav-config.module';
import { MemberModule } from './modules/member/member.module';
import { DeliveryAddressModule } from './modules/deliveryAddress/delivery-address.module';
import { CollectionModule } from './modules/collection/collection.module';
console.log("redis======", redis);

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: LoggingInterceptor,
        },
        RedisUtils
    ],
    imports: [
        ConfigModule.load(resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
        TypeOrmModule.forRootAsync({
            useFactory: (config: ConfigService) => config.get('database'),
            inject: [ConfigService],
        }),
        // module location
        PayWaterModule,
        AddressModule,
        ProductModule,
        ClassificationModule,
        CartModule,
        OrderModule,
        FrontRequestLogModule,
        MemberModule,
        NavConfigModule,
        DeliveryAddressModule,
        CollectionModule,
        RedisModule.register(redis)
    ],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer.apply(LoginMiddleware).forRoutes("/")
    }

}
